{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Bar Chart with Highlight\n",
    "------------------------\n",
    "This example shows a Bar chart that highlights values beyond a threshold.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "import pandas as pd\n",
    "\n",
    "data = pd.DataFrame({\"Day\": range(1, 16),\n",
    "                     \"Value\": [54.8, 112.1, 63.6, 37.6, 79.7, 137.9, 120.1, 103.3,\n",
    "                               394.8, 199.5, 72.3, 51.1, 112.0, 174.5, 130.5]})\n",
    "\n",
    "data2 = pd.DataFrame([{\"ThresholdValue\": 300, \"Threshold\": \"hazardous\"}])\n",
    "\n",
    "bar1 = alt.Chart(data).mark_bar().encode(\n",
    "    x='Day:O',\n",
    "    y='Value:Q'\n",
    ")\n",
    "\n",
    "bar2 = alt.Chart(data).mark_bar(color=\"#e45755\").encode(\n",
    "    x='Day:O',\n",
    "    y='baseline:Q',\n",
    "    y2='Value:Q'\n",
    ").transform_filter(\n",
    "    \"datum.Value >= 300\"\n",
    ").transform_calculate(\n",
    "    \"baseline\", \"300\"\n",
    ")\n",
    "\n",
    "rule = alt.Chart(data2).mark_rule().encode(\n",
    "    y='ThresholdValue:Q'\n",
    ")\n",
    "\n",
    "text = alt.Chart(data2).mark_text(\n",
    "    align='left', dx=215, dy=-5\n",
    ").encode(\n",
    "    alt.Y('ThresholdValue:Q', axis=alt.Axis(title='PM2.5 Value')),\n",
    "    text=alt.value('hazardous')\n",
    ")\n",
    "\n",
    "bar1 + text + bar2 + rule\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
